package com.agency8x.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// 2017-5-20
public class DateTimeUtil {

	/**
	 * 判断是否是正确的日期格式(yyyy-MM-dd)
	 * @param str
	 * @return
	 */
    public static boolean validateDate(String str) {
        return validate(str, "yyyy-MM-dd");
    }
    /**
     * 判断是否是正确的日期格式(yyyy-MM-dd HH:mm:ss)
     * @param str
     * @return
     */
    public static boolean validateDateTime(String str) {
        return validate(str, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 判断是否是正确的日期格式(yyyy-MM-dd或yyyy-MM-dd HH:mm:ss)
     * @param str
     * @param format
     * @return
     */
    public static boolean validate(String str, String format) {
    	if(null==str || str.trim().isEmpty()){
    		return false;
    	}
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            Date date = (Date) formatter.parse(str);
            return str.equals(formatter.format(date));
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 格式化SQL Server带毫秒的时间
     * @param date
     * @param format
     * @return 
     * @example formatSqlServerDateTime(getCreateTime())
     */
    public static String formatSqlServerDateTime(Date date) {
        return formatDateTime(date, "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm");
    }

    /**
     * 格式化SQL Server带毫秒的时间
     * @param date
     * @param format
     * @return 
     * @example formatSqlServerDateTime(getCreateTime(), "yyyy-MM-dd HH:mm:ss")
     */
    public static String formatSqlServerDateTime(Date date, String format) {
        return formatDateTime(date, "yyyy-MM-dd HH:mm:ss.SSS", format);
    }

    /**
     * 格式化SQLServer日期
     *
     * @param strtime
     * @param format
     * @return
     * @example formatSqlServerDateTime("2017-05-17 21:14:58.700", "yyyy年MM月dd日")
     */
    public static String formatSqlServerDateTime(String strtime, String format) {
        return formatString(strtime, "yyyy-MM-dd HH:mm:ss.SSS", format);
    }

    public static String formatDateTime(Date date, String outputformat) {
        return formatDateTime(date, null, outputformat);
    }

    public static String formatDateTime(Date date, String inputFormat, String outputformat) {
        SimpleDateFormat sdf;
        if (null == inputFormat) {
            sdf = new SimpleDateFormat();
        } else {
            sdf = new SimpleDateFormat(inputFormat);
        }
        sdf = new SimpleDateFormat(outputformat);
        return sdf.format(date);

    }

    /**
     * 格式化时间
     *
     * @param strtime
     * @param inputFormat
     * @param outputformat
     * @return
     */
    public static String formatString(String strtime, String inputFormat, String outputformat) {
        SimpleDateFormat sdf;
        if (null == inputFormat) {
            sdf = new SimpleDateFormat();
        } else {
            sdf = new SimpleDateFormat(inputFormat);
        }
        try {
            java.util.Date date = sdf.parse(strtime);
            sdf = new SimpleDateFormat(outputformat);
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

}
